home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3845 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie - I'm stuck
  5. Date: 28 Jan 1996 21:11:02 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4egop6$bn0@ns.RezoNet.NET>
  8. References: <mhoward-2701961816570001@port6.sniff.smallmedia.com>
  9. NNTP-Posting-Host: 204.19.230.7
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In referenced article, Mark Howard says...
  15. >I'm stuck.
  16.  
  17. The problem is in mixing types int and long.
  18.  
  19. num is a long, and yet you are masking it with (1 << bitCntr) an int 
  20. expression, and I assume ints are sixteen bits of your machine.  When 
  21. bitCntr is bigger than 15, the above will alwys give you zero.
  22.  
  23. Ensure that all your expressions are long, and the program will work.
  24.  
  25. If you make all your variables long, remember to change the format 
  26. string in the printf statement.
  27.  
  28. >  halfbyt[cntr1] = NULL;
  29.  
  30. You probably got a warning on this line (or would have if you had the 
  31. correct warning level set).  NULL should only be used to initialize 
  32. *pointers*.  Use 0 to initialize integer values.  (Many compiler don't 
  33. differentiate between 0 and NULL, but most modern ones define NULL as 
  34. ((void *) 0).
  35.  
  36. Instead of scanning the number bit by bit, why don't you rewrite your 
  37. loop to handle the number 4bits at a time - that's what your trying to 
  38. achieve:
  39.  
  40.     for (i = 0;  i < 8;  i++)
  41.     {
  42.       halfbyte[i] = (char)(num & 0xF);
  43.       num = num >> 4;
  44.     }
  45.  
  46. -- 
  47. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  48. Montreal                       | Phax : (514) 938 5225
  49. ray@ultimate-tech.com          | Home : (514) 630 3749
  50.  
  51.